/**
 * Created with JetBrains PhpStorm.
 * User: vander
 * Date: 5/15/13
 * Time: 1:32 AM
 * To change this template use File | Settings | File Templates.
 */
var ElementClipboard = Class.create({
    initialize: function(){
        this.type = "";
        this.data = null;
        this.class = "";
        this.cutPageId = "";
        this.isEmpty = true;

        //Can luu y la voi cach implement nay thi cho du element do co bi xoa thi no van co kha
        //nang paste sang trang khac duoc vi data cua no da duoc trich xuat ra
    },
    copy: function(ele){
        this.isEmpty = false;

        this.type = "copy";
        this.data = ele.toJSON();
        this.class = ele.className;
    },
    cut: function(ele){
        this.isEmpty = false;

        this.type = "cut";
        this.data = ele.toJSON();
        this.class = ele.className;
        this.cutPageId = ele.parent.id;
        ele.parent.removeElement(ele);
    },
    paste: function(targetPage){
        if(!this.isEmpty){
            var newEle = new window[this.class]({parent: targetPage});
            newEle.initAttributeList(this.data);

            //TH cut xong paste ve trang cu thi restore lai nguyen trang
            if(this.type == "cut"){
                if(targetPage.id == this.cutPageId){
                    //not assign new id for this element
                    targetPage.addElement(newEle, false);
                }
                else{
                    targetPage.addElement(newEle);
                }
                this.clear();
            }
            else if(this.type == "copy"){
                var left= parseFloat(newEle.getValueFromKey('left')) + 10;
                var top = parseFloat(newEle.getValueFromKey('top')) + 10;
                newEle.setValueByKey('left', left + 'px');
                newEle.setValueByKey('top', top + 'px');
                targetPage.addElement(newEle);
            }
            newEle.initEvents();
        }

    },
    clear: function(){
        this.type = "";
        this.data = null;
        this.class = "";
        this.cutPageId = "";
        this.isEmpty = true;
    }
});